/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import java.util.Iterator;
import java.util.List;
import java.lang.Double;
import java.lang.Number;
import java.lang.Object;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import static org.zkoss.zk.ui.util.Clients.log;
import org.zkoss.zul.Doublebox;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import times.TimesDAO;
import times.persistence.entities.AnimalSpecies;
import times.persistence.entities.CategoryClass;
import times.persistence.entities.PriceSlaughter;
import times.persistence.entities.PriceSource;
import times.persistence.entities.PriceSourceSlaughterHeader;
import times.persistence.entities.Category;
import times.persistence.entities.Foreas;
import times.persistence.entities.PriceSlaughterLamb;
import times.persistence.entities.PropertyPriceSource;
import times.persistence.entities.SlaughterProperty;
import org.zkoss.zul.Button;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Div;
import org.zkoss.zk.ui.event.ForwardEvent;
import times.persistence.controllers.exceptions.NonexistentEntityException;



public class PriceSlaughterLambUI extends SelectorComposer<Component> {

    @Wire
    Listbox priceSourceSlaughterHeaderLst ;
    
    @Wire
    Listbox categoryLst;
    
    @Wire
    Listbox priceSlaughterLambLst;
    
     @Wire
     Listbox foreasLst;
     
     @Wire
     Listbox animalSpeciesLst;
    
     @Wire
     Doublebox weightLambTxt;

     @Wire
     Doublebox priceLambTxt;
    
     @Wire
     Textbox representativeNameTxt;

    ListModelList<PriceSourceSlaughterHeader> priceSourceSlaughterHeaderLml;
    ListModelList<Foreas> foreasLml;
    ListModelList<Category> categoryLml;
    ListModelList<AnimalSpecies> animalSpeciesLml;

    ListModelList<PriceSlaughter> priceSlaughterLml;
    ListModelList<SlaughterProperty> slaughterPropertyLml;
    ListModelList<PropertyPriceSource> propertyPriceSourceLml;
    ListModelList<PriceSlaughterLamb> priceSlaughterLambLml;

    private Boolean newPriceSourceSlaughterHeader;
    private PriceSourceSlaughterHeader priceSourceSlaughterHeader;
    private TimesDAO tdao;
    private PriceSource priceSource;
    private boolean newPriceSlaughter;
    private PriceSlaughter priceSlaughter;
    
    private SlaughterProperty slaughterProperty;
    private PriceSlaughterLamb priceSlaughterLamb;


    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        tdao = new TimesDAO();

        // fake login
        priceSource = tdao.findPriceSourceById(1);
        
        priceSourceSlaughterHeader = tdao.findPriceSourceSlaughterHeaderById(1);
        
        // παιρνουμε ενα προβατακι καρφωτα
        AnimalSpecies animalSpecies = tdao.findAnimalSpeciesById(2);
        
// eisagogi

//        animalSpeciesLml = new ListModelList(tdao.findAnimalSpeciesEntities());
//        animalSpeciesLst.setModel(animalSpeciesLml);

        newPriceSourceSlaughterHeader = true;

        categoryLml  = new ListModelList(animalSpecies.getCategoryList());
        categoryLst.setModel(categoryLml);
        
        
        
        foreasLml = new ListModelList(tdao.findForeasEntities());
        foreasLst.setModel(foreasLml);
        
        
        priceSlaughterLambLml = new ListModelList();
        priceSlaughterLambLst.setModel(priceSlaughterLambLml);
        
    }

      @Listen("onClick = #savePrSlaughterLambBtn")
       public void onsavePrSlaughterLambBtnClick() throws Exception {
       priceSlaughterLamb = new PriceSlaughterLamb();
        getPSLUI();
        // basi
       tdao.createPriceSlaughterLamb(priceSlaughterLamb);
        // entity manager
        priceSourceSlaughterHeader.getPriceSlaughterLambList().add(priceSlaughterLamb);
        // ui
        priceSlaughterLambLml.add(priceSlaughterLamb);

        Messagebox.show("H εγγραφή αποθηκεύτηκε");
        
        clearPSLUI();

   }
       
        public void getPSLUI() {
        priceSlaughterLamb.setForeasId(foreasLml.getSelection().iterator().next());
        priceSlaughterLamb.setWeigth(weightLambTxt.doubleValue());
        priceSlaughterLamb.setPrice(priceLambTxt.doubleValue());
        priceSlaughterLamb.setRepresentativeName(representativeNameTxt.getValue());
        priceSlaughterLamb.setCategoryId(categoryLml.getSelection().iterator().next());
        
        priceSlaughterLamb.setPriceSourceSlaughterHeaderId(priceSourceSlaughterHeader);
        
    }
        
        private void setPSLUI() {
        foreasLml.addToSelection(priceSlaughterLamb.getForeasId());
        weightLambTxt.setValue(priceSlaughterLamb.getWeigth());
        priceLambTxt.setValue(priceSlaughterLamb.getPrice());
        representativeNameTxt.setValue(priceSlaughterLamb.getRepresentativeName());
        categoryLml.addToSelection(priceSlaughterLamb.getCategoryId());
    }
        
      @Listen("onSelect = #priceSlaughterLambLst")
       public void onPriceSlaughterLAmbSelect() {
        clearPSLUI();
        priceSlaughterLamb = priceSlaughterLambLml.getSelection().iterator().next();
        setPSLUI();

    }
    
        private void clearPSLUI() {
        weightLambTxt.setRawValue("");
        priceLambTxt.setRawValue("");
        representativeNameTxt.setRawValue("");
      }
        
      @Listen("onPriceSlaughterLambDelete = #priceSlaughterLambLst")
        public void onPriceSlaughterLambDelete(ForwardEvent event) throws NonexistentEntityException {
 
       Button button  = (Button) event.getOrigin().getTarget();
        
        Listitem listitem = (Listitem) button.getParent().getParent(); 
        
        priceSlaughterLamb =  listitem.getValue();
       
        
       // diagrafi apo listmodel
         priceSlaughterLambLml.remove( priceSlaughterLamb);

        // basi
        tdao.deletePriceSlaughterLamb(priceSlaughterLamb.getId());
       
        Messagebox.show("H εγγραφή διαγράφτηκε");
        
        clearPSLUI();
    }     
        
}
